<?php
namespace app\index\controller;

use app\index\model\ContractBuyModel;
use app\index\model\ContractCaiModel;
use app\index\model\EnginModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\StuffChuModel;
use app\index\model\StuffRuListModel;
use app\index\model\StuffRuModel;
use app\index\model\StuffTuiModel;
use app\index\relevan\EnginRelevan;
use app\index\service\BianhaoService;
use app\index\service\EnginService;
use app\index\service\FlowService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\StuffService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Stuff extends Common
{
    public function mingxi_stuff_ru(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=', 1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['ru.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['ru.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ru.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['rudt']) && !empty($param['rudt'])){
            $where[] = ['ru.rudt','between', explode(' - ', $param['rudt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['ru.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['rutype']) && $param['rutype'] != ''){
            $where[] = ['ru.rutype','=', $param['rutype']];
        }
        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['rutype'] = (new StuffService())->get_stuff_ru_rutype();
        $data['is_rutype'] = isset($param['rutype']) ? $param['rutype'] : '';


        $data['data'] = StuffRuListModel::alias('list')
            ->field('ru.*,e.name as enginname,cai.name as cainame,buy.title as buytitle,list.id as listid
            ,list.name as listname,list.unit as listunit,list.price as listprice,list.num as listnum,list.money as listmoney')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->join('engin e','e.id=ru.enginid')
            ->leftJoin('contract_cai cai','cai.id=ru.caiid and ru.rutype=1')
            ->leftJoin('contract_buy buy','buy.id=ru.buyid and ru.rutype=2')
            ->where($where)
            ->where((new MenuService())->get_miss_where('mingxi_stuff_ru', 'ru'))
            ->order('ru.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['rutype_name'] = (new StuffService())->get_stuff_ru_rutype_name($item['rutype']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_stuff_ru(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['ru.enginid','=', $param['enginid']];
        }
        if(isset($param['caiid']) && !empty($param['caiid'])){
            $where[] = ['ru.caiid','=', $param['caiid']];
        }
        if(isset($param['buyid']) && !empty($param['buyid'])){
            $where[] = ['ru.buyid','=', $param['buyid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['ru.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['ru.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['rudt']) && !empty($param['rudt'])){
            $where[] = ['ru.rudt','between', explode(' - ', $param['rudt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['ru.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['ru.status','=', $param['status']];
        }
        if(isset($param['rutype']) && !empty($param['rutype'])){
            $where[] = ['ru.rutype','=', $param['rutype']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['rutype'] = (new StuffService())->get_stuff_ru_rutype();
        $data['is_rutype'] = isset($param['rutype']) ? $param['rutype'] : '';


        $data['data'] = StuffRuModel::alias('ru')
            ->field('ru.*,e.name as enginname,cai.name as caiid_name,bill.sericnum,bill.modename')
            ->join('engin e','e.id=ru.enginid')
            ->leftJoin('contract_cai cai','cai.id=ru.caiid')
            ->leftJoin('flow_bill bill','bill.id=ru.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('stuff_ru', 'ru'))
            ->order('ru.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['rutype_name'] = (new StuffService())->get_stuff_ru_rutype_name($item['rutype']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('stuff_ru');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_stuff_ru(Request $request){
        $param = $request->post();
        $data = StuffRuModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            StuffRuModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new EnginRelevan())->vate_stuff_ru_list_and_caiorbuy($data['id']);
            (new EnginRelevan())->vate_stuff_ru_list_ben($data['id']);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'stuff_ru', $data['id'], '材料入库单');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_stuff_ru(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = StuffRuModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['rutype_name'] = (new StuffService())->get_stuff_ru_rutype_name($data['data']['rutype']);

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $data['buyidinfo'] = ContractBuyModel::where(['id'=>$data['data']['buyid']])->find();
        $data['caiidinfo'] = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();

        $list = StuffRuListModel::where(['ruid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('stuff_ru', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function mingxi_stuff_chu(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=', 1];

        if(isset($param['enginid']) && $param['enginid'] != ''){
            $where[] = ['ru.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['chu.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['chu.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chudt']) && !empty($param['chudt'])){
            $where[] = ['chu.chudt','between', explode(' - ', $param['chudt'])];
        }
        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';



        $data['data'] = StuffChuModel::alias('chu')
            ->field('chu.*,e.name as enginname,list.name as listname,list.unit as listunit,ru.gonger')
            ->join('stuff_ru_list list','list.id=chu.ruid')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->join('engin e','e.id=ru.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('mingxi_stuff_chu', 'ru'))
            ->order('chu.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_stuff_chu(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['chu.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=', 1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['ru.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['chu.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['chu.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chudt']) && !empty($param['chudt'])){
            $where[] = ['chu.chudt','between', explode(' - ', $param['chudt'])];
        }
        if(isset($param['created_user_name']) && !empty($param['created_user_name'])){
            $where[] = ['u.realname','like', '%'.$param['created_user_name'].'%'];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';



        $data['data'] = StuffChuModel::alias('chu')
            ->field('chu.*,e.name as enginname,u.realname as created_user_name,bill.sericnum,bill.modename')
            ->join('engin e','e.id=chu.enginid')
            ->join('stuff_ru_list list','list.id=chu.ruid')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->join('user u','u.id=chu.created_user')
            ->leftJoin('flow_bill bill','bill.id=ru.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('stuff_ru', 'ru'))
            ->group('chu.number')
            ->order('chu.number DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('stuff_chu');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_stuff_chu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }

            if(empty($param['chudt'])){
                return outputJson(4000, '请选择出库日期');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['deptid'] = $this->userinfo['deptid'];
            $param['number'] = (new BianhaoService())->create_stuff_chu_number();
            $param['created_user'] = $this->userinfo['id'];
            $param['created_at'] = date('Y-m-d H:i:s');



            $list = array_filter($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入出库明细');
            }


            foreach ($list as &$item){
                $item['comid'] = $param['comid'];
                $item['chanid'] = $param['chanid'];
                $item['deptid'] = $param['deptid'];
                $item['enginid'] = $param['enginid'];
                $item['number'] = $param['number'];
                $item['chudt'] = $param['chudt'];
                $item['baner'] = $param['baner'];
                $item['created_user'] = $param['created_user'];
                $item['created_at'] = $param['created_at'];
            }


            Db::startTrans();
            try {
                foreach($list as $v){
                    StuffChuModel::insert($v);
                    (new EnginRelevan())->vate_stuff_chutui($v['ruid']);
                }



                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['number'] = (new BianhaoService())->create_stuff_chu_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_stuff_chu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $list = array_filter($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入出库明细');
            }

            $chuinfo = StuffChuModel::where(['id'=>$param['id']])->find();

            $param['comid'] = $chuinfo['comid'];
            $param['chanid'] = $chuinfo['chanid'];
            $param['deptid'] = $chuinfo['deptid'];
            $param['enginid'] = $chuinfo['enginid'];
            $param['number'] = $chuinfo['number'];
            $param['created_user'] = $chuinfo['created_user'];
            $param['created_at'] = $chuinfo['created_at'];
            Db::startTrans();
            try {

                StuffChuModel::where(['number'=>$param['number']])->delete();
                foreach($list as $v){
                    StuffChuModel::insert([
                        'comid' => $param['comid'],
                        'chanid' => $param['chanid'],
                        'deptid' => $param['deptid'],
                        'enginid' => $param['enginid'],
                        'number' => $param['number'],
                        'created_user' => $param['created_user'],
                        'created_at' => $param['created_at'],
                        'chudt' => $param['chudt'],
                        'baner' => $param['baner'],
                        'ruid' => $v['ruid'],
                        'num' => $v['num'],
                        'explain' => $v['explain'],
                    ]);

                    (new EnginRelevan())->vate_stuff_chutui($v['ruid']);
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->param('id', 0, 'int');
            $data['data'] = StuffChuModel::where(['id'=>$id])->find();


            $engininfo = EnginModel::where(['id'=>$data['data']['enginid']])->find();
            $data['engininfo'] = $engininfo;


            $list = StuffChuModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['enginid'],'number'=>$data['data']['number']])->select()->toArray();
            foreach ($list as &$item){
                $rulistinfo = StuffRuListModel::where(['id'=>$item['ruid']])->find();
                $ruinfo = StuffRuModel::where(['id'=>$rulistinfo['ruid']])->find();
                $chunum = StuffChuModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');
                $chunum -= $item['num'];

                $tuinum = StuffTuiModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');
                $cunnum = $rulistinfo['num'] - $chunum + $tuinum;

                $item['name'] = $rulistinfo['name'];
                $item['unit'] = $rulistinfo['unit'];
                $item['gonger'] = $ruinfo['gonger'];
                $item['runum'] = $rulistinfo['num'];
                $item['chunum'] = $chunum;
                $item['cunnum'] = $cunnum;
            }
            $data['list'] = $list;
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function del_stuff_chu(Request $request){
        $param = $request->post();
        $data = StuffChuModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            StuffChuModel::where(['id'=>$data['id']])->delete();
            (new EnginRelevan())->vate_stuff_chutui($data['ruid']);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_stuff_chu(Request $request){
        $id = $request->param('id', 0, 'int');
        $data['data'] = StuffChuModel::where(['id'=>$id])->find();


        $engininfo = EnginModel::where(['id'=>$data['data']['enginid']])->find();
        $data['engininfo'] = $engininfo;


        $list = StuffChuModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['enginid'],'number'=>$data['data']['number']])->select()->toArray();
        foreach ($list as &$item){
            $rulistinfo = StuffRuListModel::where(['id'=>$item['ruid']])->find();
            $ruinfo = StuffRuModel::where(['id'=>$rulistinfo['ruid']])->find();
            $chunum = StuffChuModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');
            $chunum -= $item['num'];

            $tuinum = StuffTuiModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');
            $cunnum = $rulistinfo['num'] - $chunum + $tuinum;

            $item['name'] = $rulistinfo['name'];
            $item['unit'] = $rulistinfo['unit'];
            $item['gonger'] = $ruinfo['gonger'];
            $item['runum'] = $rulistinfo['num'];
            $item['chunum'] = $chunum;
            $item['cunnum'] = $cunnum;
        }
        $data['list'] = $list;
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function mingxi_stuff_tui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=', 1];

        if(isset($param['enginid']) && $param['enginid'] != ''){
            $where[] = ['ru.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['tui.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['tui.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['tuidt']) && !empty($param['tuidt'])){
            $where[] = ['tui.tuidt','between', explode(' - ', $param['tuidt'])];
        }
        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';



        $data['data'] = StuffTuiModel::alias('tui')
            ->field('tui.*,e.name as enginname,list.name as listname,list.unit as listunit,ru.gonger')
            ->join('stuff_ru_list list','list.id=tui.ruid')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->join('engin e','e.id=ru.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('mingxi_stuff_tui', 'ru'))
            ->order('tui.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_stuff_tui(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['tui.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=', 1];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['ru.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['tui.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['tui.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['tuidt']) && !empty($param['tuidt'])){
            $where[] = ['tui.tuidt','between', explode(' - ', $param['tuidt'])];
        }
        if(isset($param['created_user_name']) && !empty($param['created_user_name'])){
            $where[] = ['u.realname','like', '%'.$param['created_user_name'].'%'];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';



        $data['data'] = StuffTuiModel::alias('tui')
            ->field('tui.*,e.name as enginname,u.realname as created_user_name,bill.sericnum,bill.modename')
            ->join('engin e','e.id=tui.enginid')
            ->join('stuff_ru_list list','list.id=tui.ruid')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->join('user u','u.id=tui.created_user')
            ->leftJoin('flow_bill bill','bill.id=ru.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('stuff_ru', 'ru'))
            ->group('tui.number')
            ->order('tui.number DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('stuff_tui');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_stuff_tui(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }

            if(empty($param['tuidt'])){
                return outputJson(4000, '请选择退库日期');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $param['deptid'] = $this->userinfo['deptid'];
            $param['number'] = (new BianhaoService())->create_stuff_tui_number();
            $param['created_user'] = $this->userinfo['id'];
            $param['created_at'] = date('Y-m-d H:i:s');



            $list = array_filter($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入退库明细');
            }


            foreach ($list as &$item){
                $item['comid'] = $param['comid'];
                $item['chanid'] = $param['chanid'];
                $item['deptid'] = $param['deptid'];
                $item['enginid'] = $param['enginid'];
                $item['number'] = $param['number'];
                $item['tuidt'] = $param['tuidt'];
                $item['baner'] = $param['baner'];
                $item['created_user'] = $param['created_user'];
                $item['created_at'] = $param['created_at'];
            }



            Db::startTrans();
            try {
                foreach($list as $v){
                    StuffTuiModel::insert($v);
                    (new EnginRelevan())->vate_stuff_chutui($v['ruid']);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['number'] = (new BianhaoService())->create_stuff_tui_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_stuff_tui(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $list = array_filter($param['list']);
            if(count($list) < 1){
                return outputJson(4000, '请输入退库明细');
            }


            $tuiinfo = StuffTuiModel::where(['id'=>$param['id']])->find();

            $param['comid'] = $tuiinfo['comid'];
            $param['chanid'] = $tuiinfo['chanid'];
            $param['deptid'] = $tuiinfo['deptid'];
            $param['enginid'] = $tuiinfo['enginid'];
            $param['number'] = $tuiinfo['number'];
            $param['created_user'] = $tuiinfo['created_user'];
            $param['created_at'] = $tuiinfo['created_at'];

            Db::startTrans();
            try {

                StuffTuiModel::where(['number'=>$param['number']])->delete();
                foreach($list as $v){
                    StuffTuiModel::insert([
                        'comid' => $param['comid'],
                        'chanid' => $param['chanid'],
                        'deptid' => $param['deptid'],
                        'enginid' => $param['enginid'],
                        'number' => $param['number'],
                        'created_user' => $param['created_user'],
                        'created_at' => $param['created_at'],
                        'tuidt' => $param['tuidt'],
                        'baner' => $param['baner'],
                        'ruid' => $v['ruid'],
                        'num' => $v['num'],
                        'explain' => $v['explain'],
                    ]);
                    (new EnginRelevan())->vate_stuff_chutui($v['ruid']);
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->param('id', 0, 'int');
            $data['data'] = StuffTuiModel::where(['id'=>$id])->find();


            $engininfo = EnginModel::where(['id'=>$data['data']['enginid']])->find();
            $data['engininfo'] = $engininfo;


            $list = StuffTuiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['enginid'],'number'=>$data['data']['number']])->select()->toArray();
            foreach ($list as &$item){
                $rulistinfo = StuffRuListModel::where(['id'=>$item['ruid']])->find();
                $ruinfo = StuffRuModel::where(['id'=>$rulistinfo['ruid']])->find();
                $chunum = StuffChuModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');
                $tuinum = StuffTuiModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$rulistinfo['id']])->sum('num');
                $tuinum -= $item['num'];


                $item['name'] = $rulistinfo['name'];
                $item['unit'] = $rulistinfo['unit'];
                $item['gonger'] = $ruinfo['gonger'];
                $item['runum'] = $rulistinfo['num'];
                $item['chunum'] = $chunum;
                $item['tuinum'] = $tuinum;
            }
            $data['list'] = $list;
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function del_stuff_tui(Request $request){
        $param = $request->post();
        $data = StuffTuiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            StuffTuiModel::where(['id'=>$data['id']])->delete();
            (new EnginRelevan())->vate_stuff_chutui($data['ruid']);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_stuff_tui(Request $request){
        $id = $request->param('id', 0, 'int');
        $data['data'] = StuffTuiModel::where(['id'=>$id])->find();


        $engininfo = EnginModel::where(['id'=>$data['data']['enginid']])->find();
        $data['engininfo'] = $engininfo;


        $list = StuffTuiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$data['data']['enginid'],'number'=>$data['data']['number']])->select()->toArray();
        foreach ($list as &$item){
            $rulistinfo = StuffRuListModel::where(['id'=>$item['ruid']])->find();
            $ruinfo = StuffRuModel::where(['id'=>$rulistinfo['ruid']])->find();
            $item['name'] = $rulistinfo['name'];
            $item['unit'] = $rulistinfo['unit'];
            $item['gonger'] = $ruinfo['gonger'];
            $item['runum'] = $rulistinfo['num'];
            $item['chunum'] = $rulistinfo['num'];
            $item['tuinum'] = $rulistinfo['num'];
        }
        $data['list'] = $list;
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function detail_stuff_ku(Request $request){
        $param = $request->post();
        $chatype = $request->post('chatype', 0, 'int');

        $data = [];

        $listinfo = StuffRuListModel::where('id','in',$param['idstr'])->select()->toArray();
        foreach ($listinfo as $item){
            $ruinfo = StuffRuModel::where(['id'=>$item['ruid']])->find();
            $enginname = EnginModel::where(['id'=>$ruinfo['enginid']])->value('name');
            $caiid_name = ContractCaiModel::where(['id'=>$ruinfo['caiid']])->value('name');

            $init = [
                'type' => 'ru',
                'id' => $item['id'],
                'number' => $ruinfo['number'],
                'dt' => $ruinfo['rudt'],
                'dongname' => '入库',
                'name' => $item['name'],
                'unit' => $item['unit'],
                'price' => $item['price'],
                'num' => $item['num'],
                'money' => $item['money'],
                'baner' => $ruinfo['baner'],
                'gonger' => $ruinfo['gonger'],
                'enginname' => $enginname,
                'caiid_name' => $caiid_name,
            ];
            if($chatype == 0 || $chatype == 1){
                $data[] = $init;
            }



            if($chatype == 0 || $chatype == 2){
                $chu = StuffChuModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$item['id']])->select()->toArray();
                foreach ($chu as $v){
                    $init_chu = $init;
                    $init_chu['type'] = 'chu';
                    $init_chu['id'] = $v['id'];
                    $init_chu['number'] = $v['number'];
                    $init_chu['dt'] = $v['chudt'];
                    $init_chu['dongname'] = '出库';
                    $init_chu['num'] = $v['num'];
                    $init_chu['money'] = $init_chu['price']*$v['num'];
                    $init_chu['baner'] = $v['baner'];
                    $data[] = $init_chu;
                }
            }


            if($chatype == 0 || $chatype == 3){
                $tui = StuffTuiModel::where(['enginid'=>$ruinfo['enginid'],'ruid'=>$item['id']])->select()->toArray();
                foreach ($tui as $v){
                    $init_chu = $init;
                    $init_chu['type'] = 'tui';
                    $init_chu['id'] = $v['id'];
                    $init_chu['number'] = $v['number'];
                    $init_chu['dt'] = $v['tuidt'];
                    $init_chu['dongname'] = '退库';
                    $init_chu['num'] = $v['num'];
                    $init_chu['money'] = $init_chu['price']*$v['num'];
                    $init_chu['baner'] = $v['baner'];
                    $data[] = $init_chu;
                }
            }



        }

        $dtSort = array_column($data,'dt');
        array_multisort($dtSort,SORT_ASC,$data);

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_stuff_ku(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ru.deleted_user','=', 0];
        $where[] = ['ru.comid','=',$this->userinfo['comid']];
        $where[] = ['ru.isturn','=',1];
        $where[] = ['ru.status','=', 1];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['listname']) && !empty($param['listname'])){
            $where[] = ['list.name','like', '%'.$param['listname'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['ru.enginid','=', $param['enginid']];
        }

        if(isset($param['isku']) && !empty($param['isku'])){
            if($param['isku'] == 1){
                $where[] = ['list.cunnum','>', 0];
            }elseif($param['isku'] == 2){
                $where[] = ['list.cunnum','=', 0];
            }

        }

        $list = [];
        $info = StuffRuListModel::alias('list')
            ->field('list.name,list.unit,sum(list.num) as listnum,sum(list.money) as listmoney
            ,sum(list.chunum) as listchunum,sum(list.chumoney) as listchumoney
            ,sum(list.tuinum) as listtuinum,sum(list.tuimoney) as listtuimoney
            ,sum(list.yongnum) as listyongnum,sum(list.yongmoney) as listyongmoney
            ,sum(list.cunnum) as listcunnum,sum(list.cunmoney) as listcunmoney
            ,GROUP_CONCAT(list.id) as idstr,GROUP_CONCAT(list.price) as pricestr,ru.enginid,bill.sericnum,bill.modename')
            ->join('stuff_ru ru','ru.id=list.ruid')
            ->join('engin e','e.id=ru.enginid')
            ->leftJoin('flow_bill bill','bill.id=ru.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('stuff_ku', 'ru'))
            ->order('list.name ASC')
            ->group('list.name,list.unit')
            ->select()->toArray();

        foreach ($info as &$item){
            $item['enginid_name'] = EnginModel::where(['id'=>$item['enginid']])->value('name');
            if(isset($param['isku']) && $param['isku'] == 1){
                if($item['listcunnum'] > 0){
                    $list[] = $item;
                }
            }elseif(isset($param['isku']) && $param['isku'] == 2){
                if($item['listcunnum'] <= 0){
                    $list[] = $item;
                }
            }else{
                $list[] = $item;
            }
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }





























}
